package com.wanmait.movie.controller;

import com.wanmait.movie.entity.Admin;
import com.wanmait.movie.entity.Screening;
import com.wanmait.movie.entity.VO.ScreeningDTO;
import com.wanmait.movie.service.CinemaService;
import com.wanmait.movie.service.LobbyService;
import com.wanmait.movie.service.MovieService;
import com.wanmait.movie.service.ScreeningService;
import com.wanmait.movie.util.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("manage/screening")
public class ManageScreeningController {
    @Resource
    private ScreeningService screeningService;
    @Resource
    private CinemaService cinemaService;
    @Resource
    private LobbyService lobbyService;
    @Resource
    private MovieService movieService;

    @GetMapping("list")
    public AjaxResult list(Integer pageNum, Integer cinemaId, Integer lobbyId, String keyword) {
        if (pageNum == null){
            pageNum = 1;
        }
        if (cinemaId == null){
            cinemaId = 0;
        }
        if (lobbyId == null){
            lobbyId = 0;
        }
        if (keyword == null) {
            keyword = "";
        }
        return AjaxResult.success(screeningService.getScreeningList(pageNum,cinemaId,lobbyId,keyword)).put("cinemaList", cinemaService.getCinemaList());
    }

    @GetMapping("findMovieListAndCinemaList")
    public AjaxResult findMovieListAndCinemaList(){
        return AjaxResult.success(cinemaService.getCinemaList()).put("movieList", movieService.getMovieList());
    }

    @GetMapping("findLobbyListByCinemaId")
    public AjaxResult findLobbyListByCinemaId(Integer id){
        if (id==null){
            id=1;
        }
        return AjaxResult.success(lobbyService.getLobbyListByCinemaId(id));
    }

    @GetMapping("delete")
    public AjaxResult delete(Integer id){
        screeningService.removeById(id);
        return AjaxResult.success("删除成功");
    }

    @PostMapping("add")
    public AjaxResult add(@RequestBody Screening screening, HttpServletRequest request){
        Admin admin = (Admin) request.getAttribute("admin");
        if (screening.getId() == null){
            screening.setInsertAdminId(admin.getId()).setUpdateAdminId(admin.getId()).setUpdateTime(new Date());
        }else {
            screening.setUpdateAdminId(admin.getId()).setUpdateTime(new Date());
        }
        return AjaxResult.success(screeningService.saveOrUpdate(screening));
    }

    @GetMapping("getScreeningInfo")
    public AjaxResult getScreeningInfo(Integer id){
        Screening screening=screeningService.getScreeningInfo(id);
        int cinemaId = lobbyService.getCinemaIdByLobbyId(screening.getLobbyId());
        return AjaxResult.success(screening).put("cinemaId", cinemaId);
    }
}
